package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;

import communication.*;


public class FileAcceptor {

	private static String fileReceived;
	
	public static void main(String[] args) {
	
		try {
			ServerConnection conn;
			
			conn = new ServerConnection(4444);
			
			SocketConnection socket = conn.accept();
			
			DataInputStream in = new DataInputStream(socket.getInputStream());
	    	
	    	fileReceived = in.readUTF();
	    	
	    	String fileExt = fileReceived.substring(fileReceived.indexOf("."), fileReceived.length());
	    	
	    	//System.out.println(fileExt);
	    	
	    	
    	  	BufferedInputStream buffIn = new BufferedInputStream(socket.getInputStream());
	    	
	    	BufferedOutputStream buffOt = new BufferedOutputStream(new FileOutputStream(new File("TEST2" + fileExt)));
	    	
	    	byte [] b = new byte[4096];
	    	int bytesRead;
	    	
	    	//While there's still something left to read....
	    	while ((bytesRead = buffIn.read(b)) != -1) {
	    		buffOt.write(b,0,bytesRead);
	    	}
	    	buffOt.flush();

	    	buffOt.close();
	    	buffIn.close();
	    	


		
	}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
}
